package logMonitor.dao;

import logMonitor.domain.Message;
import logMonitor.domain.Record;
import logMonitor.domain.Rule;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2018/1/11.
 */
public class LogMonitorDao {
    private static Logger logger = Logger.getLogger(LogMonitorDao.class);
    private JdbcTemplate jdbcTemplate;

    public LogMonitorDao() {
        jdbcTemplate = new JdbcTemplate(DataSourceUtil.getDataSource());
    }


    public void deleteRecordByRuleId(Integer personId) {
        jdbcTemplate .update( "delete from `log_monitor`.`record` where `ruleId`=?" ,
                new Object[] { personId },
                new int [] { java.sql.Types. INTEGER });
    }
    public int[][] saveRecord(List<Record> recordList) {
        String sql = "INSERT INTO `log_monitor`.`record` (`ruleId`,`src_ip`,`dst_ip`,`creatTime`,`value`) " +
                "VALUES (?,?,?,?,?)";
        return saveAppendRecord(recordList, sql);
    }

    public int[][] saveAppendRecord(List<Record> recordList, String sql) {
        return jdbcTemplate.batchUpdate(sql, recordList, recordList.size(), new ParameterizedPreparedStatementSetter<Record>() {
            @Override
            public void setValues(PreparedStatement ps, Record argument) throws SQLException {
                ps.setInt(1, argument.getRuleId());
                ps.setString(2, argument.getSrc_ip());
                ps.setString(3, argument.getDst_ip());
                ps.setString(4, argument.getCreatTime());
                ps.setDouble(5, argument.getValue());
            }
        });
    }
    public  List<Rule> loadRuleAlive() {
        String sql = "SELECT * " +
                " FROM `log_monitor`.`rule` AS job " +
                " WHERE job.`isValid` =1 and job.`isExist` = 1";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Rule>(Rule.class));
    }
    public List<Rule> loadRuleDead() {
        String sql = "SELECT * " +
                " FROM `log_monitor`.`rule` AS job " +
                " WHERE job.`isValid` =0 OR job.`isExist` = 0";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Rule>(Rule.class));
    }

    //已经放弃使用，数据库不再使用message表
    public int[][] saveMessage(List<Message> messageList){
        String sql = "INSERT INTO `log_monitor`.`message` (`src_ip`,`dst_ip`,`time`,`flag`,`src_port`,`dst_port`,`pro`,`ipid`,`seq`,`ack` ) " +
                "VALUES (?,?,?,?,?,?,?,?,?,?)";
        return saveAppendMessage(messageList, sql);
    }
    //已经放弃使用，数据库不再使用message表
    public int[][] saveAppendMessage(List<Message> messageList, String sql) {
        return jdbcTemplate.batchUpdate(sql, messageList, messageList.size(), new ParameterizedPreparedStatementSetter<Message>() {
            @Override
            public void setValues(PreparedStatement ps, Message argument) throws SQLException {
                ps.setString(1, argument.getSrc_ip());
                ps.setString(2, argument.getDst_ip());
                ps.setString(3, argument.getTime());
                ps.setString(4, argument.getFlag());
                ps.setString(5, argument.getSrc_port());
                ps.setString(6, argument.getDst_port());
                ps.setString(7, argument.getPro());
                ps.setString(8, argument.getId());
                ps.setString(9, argument.getSeq());
                ps.setString(10, argument.getAck());
            }
        });
    }
}
